package com.simple.mq.boot.starter.util;

import com.simple.mq.boot.starter.config.SimpleMqCommonConfig;
import com.simple.mq.sdk.producer.SimpleMqProducer;

import java.net.URI;

/**
 * 项目: simple-mq
 *
 * 功能描述:
 *
 * @author: WuChengXing
 *
 * @create: 2023-10-07 19:15
 **/
public class MqSendUtil {

    private final SimpleMqCommonConfig simpleMqCommonConfig;

    public MqSendUtil(SimpleMqCommonConfig simpleMqCommonConfig) {
        this.simpleMqCommonConfig = simpleMqCommonConfig;
    }

    private SimpleMqProducer simpleMqProducer;

    public void init() {
        simpleMqProducer = new SimpleMqProducer(URI.create(transfer()), Thread.currentThread().getName());
    }

    private String transfer() {
        return "ws://" + simpleMqCommonConfig.getHost() + ":" + simpleMqCommonConfig.getPort() + "/";
    }

    public void send(Object message, String routingKey) {
        simpleMqProducer.send(message, routingKey);
    }
}
